//
//  UIButton+exchange.swift
//  weiboxiangmu
//
//  Created by wei_jingyun on 15/7/9.
//  Copyright © 2015年 wei_jingyun. All rights reserved.
//

import UIKit

class UIButtonExchange: UIButton {

    class func button(title: String , imageName : String , highlightedImageName: String? = nil, target: AnyObject? = nil, actionName: String? = nil ) -> UIButtonExchange {
    
    let btn = UIButtonExchange()
    btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    btn.setTitle(title + " ", forState: UIControlState.Normal)
    btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
    let hImageName = highlightedImageName ?? imageName + "_highlighted"
    btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
    // 添加监听方法
    if actionName != nil {
        btn.addTarget(target, action: Selector(actionName!), forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    btn.sizeToFit()
    
    return btn
}
    


override func layoutSubviews() {
    super.layoutSubviews()
    
    // swift 中，能够直接修改 frame 内部数值
    titleLabel!.frame.origin.x = 0
    imageView!.frame.origin.x = titleLabel!.bounds.width
}

}
